#include <stdio.h>
#include <stdlib.h>

#define MAXN 30
typedef int ElementType;

typedef struct TNode* BinTree;
struct TNode {
    ElementType Data;
    BinTree Left;
    BinTree Right;
};

BinTree BuildTree ( int Inorder[], int Postorder[], int N );
void PreorderTraversal ( BinTree BT );
void free_treein(BinTree BT);

int main () {
    BinTree BT;
    int Inorder[MAXN], Postorder[MAXN], N, i;
    scanf("%d",&N);
    for ( i = 0; i < N; i++ ) scanf("%d",&Postorder[i]);
    for ( i = 0; i < N; i++ ) scanf("%d",&Inorder[i]);
    BT = BuildTree( Inorder, Postorder, N );
    printf("Preorder:");
    PreorderTraversal(BT);
    printf("\n");
    free_treein(BT);
    return 0;
}

BinTree BuildTree ( int Inorder[], int Postorder[], int N ) {
    if ( !N ) return NULL;
    int i = 0;
    BinTree BT = (BinTree)malloc(sizeof(struct TNode));
    BT->Data = Postorder[N-1];
    BT->Left = BT->Right = NULL;
    for ( i = 0; i < N; i++ ) if ( Inorder[i] == Postorder[N-1] ) break;
    BT->Left = BuildTree(Inorder, Postorder, i);
    BT->Right = BuildTree(Inorder+i+1, Postorder+i, N-i-1);
    return BT;
}

void PreorderTraversal ( BinTree BT ) {
    if (BT) {
        BinTree tmp = BT;
        printf("%d ",BT->Data);
        PreorderTraversal(BT->Left);
        PreorderTraversal(BT->Right);
    }
}

void free_treein ( BinTree BT ) {
    if (BT) {
        BinTree tmp = BT;
        free_treein(BT->Left);
        free(tmp);
        printf("free success\n");
        free_treein(BT->Right);
    }
}
/* Pre Unable! */
void free_treepre ( BinTree BT ) {
    if (BT) {
        BinTree tmp = BT;
        free(tmp);
        printf("free success\n");
        free_treepre(BT->Left);
        free_treepre(BT->Right);
    }
}
void free_treepos ( BinTree BT ) {
    if (BT) {
        free_treepos(BT->Left);
        free_treepos(BT->Right);
        BinTree tmp = BT;
        free(tmp);
        printf("free success\n");
        
    }
}